let app = getApp();
import { getIndexList, getCategoryTitle, getIndexRecomList } from "../../api";
import { baseImgUrl, programTitle } from "../../setting";
Page({
  data: {
    baseurl_img: baseImgUrl,
    allInfo: [],
    fixedTop: false,
    topCity: '全国',
    advertiseList: [],
    indexPage: 1,
    pageSize: 8,
    indexTotal: 0,
    scrollList: [],
    selectCateId: 1,
    recomPage: 1,
    recommendList: [],
    recommendTotal: 0,
    canReachBottom: true,
    labelList: [],
    systemInfo: {}
  },
  onLoad(query) {
    my.setNavigationBar({ title: programTitle });
    // 页面加载
    this.getData();
    this.getRecomList();
    this.getCateTitle()
    my.getSystemInfo({
      success: (res) => {
        this.setData({
          systemInfo: res
        })
      }
    })
  },
  onShow() {
    setTimeout(() => { my.hideLoading() }, 1000);
  },
  getData(flag) {
    const { indexPage, allInfo } = this.data
    getIndexList({
      page: flag ? indexPage + 1 : 1,
    }).then(res => {
      my.stopPullDownRefresh()
      let indexData = res.data
      indexData.forEach(item => {
        item.params = JSON.parse(item.params)
      })
      let list = [...allInfo]
      if (flag) {
        list = [...list, ...indexData]
      } else {
        list = [...indexData]
      }
      this.setData({
        allInfo: list,
        indexPage: res.current_page,
        indexTotal: res.total
      })
    }).catch(err => {
      my.stopPullDownRefresh();
    })
  },
  onPageScroll(e) {
    const that = this;
    if (e.scrollTop > 200) {
      that.setData({ fixedTop: true })
    } else {
      that.setData({ fixedTop: false })
    }
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: programTitle,
      desc: '信用免押一站式租赁服务平台',
      path: 'pages/index/index',
    };
  },
  onClickJump(e) {
    const { jumpType, jumpUrl } = e.info
    if (jumpType == 1) {
      if (jumpUrl.indexOf('plugin') > -1) {
        my.navigateTo({ url: jumpUrl });
      } else {
        my.navigateTo({ url: '../../' + jumpUrl })
      }
      // my.navigateTo({ url: '../webView/webView?jumpUrl=' + jumpUrl + '&jumpType=' + jumpType })
    } else if (jumpType == 2) {
      my.navigateTo({ url: '../../pagesA/pages/productDetails/productDetails?goodsId=' + e.info.goods.goods_id+'&sale_user_id='+app.globalData.sale_user_id })
    }
  },
  onJumpDetails(e) {
    let id
    if (e.info && e.info.goods_id) {
      id = e.info.goods_id
    } else {
      id = e.currentTarget.dataset.goods_id || e.currentTarget.dataset.info.goods_id
    }
    if (id) {
      my.navigateTo({
        url: '../../pagesA/pages/productDetails/productDetails?goodsId=' + id+'&sale_user_id='+app.globalData.sale_user_id
      })
    }
  },
  jumpHuBei() {
  },
  getCateTitle() {
    getCategoryTitle().then(res => {
      if (res.length) {
        this.setData({
          scrollList: res,
          selectCateId: this.data.selectCateId
        })
      }
    })
  },
  getRecomList(flag) {
    const { selectCateId, recomPage, recommendList } = this.data
    getIndexRecomList({
      cateId: selectCateId,
      page: flag ? recomPage + 1 : 1,
    }).then(res => {
      my.stopPullDownRefresh();
      if (res.data.length > 0) {
        let list = [...recommendList]
        if (flag) {
          list = [...list, ...res.data];
        } else {
          list = res.data;
        }
        list.forEach(item => {
          if (item.rent_label) {
            const arr = item.rent_label.replace(/,/g, '，').split('，').filter((p, idx) => p != '' && idx < 2);
            item['labelList'] = arr
          }
        })
        this.setData({
          recommendList: list,
          recommendTotal: res.total,
          recomPage: res.current_page,
        })
      }
    }).catch(err => {
      my.stopPullDownRefresh();
    })
  },
  seleteScroll(e) {
    let id = e.currentTarget.dataset.id
    if (id) {
      this.setData({
        selectCateId: id,
        recommendList: [],
      })
      this.getRecomList()
    }
  },
  selectCity() {
    const that = this
    my.getLocation({
      type: 1,
      success(res) {
        that.setData({
          topCity: res.city.slice(0, 2)
        })
        my.chooseCity({
          showHotCities: true,
          setLocatedCity: true,
          success: (res) => {
            that.setData({
              topCity: res.city
            })
          },
        });
      },
    });
  },
  searchTo() {
    my.navigateTo({ url: '../../pagesA/pages/search/search' })
  },
  toItemClassify(e) {
    let url = e.currentTarget.dataset.jumpUrl
    if (url) {
      // my.navigateTo({ url: '../../' + url })
      my.navigateTo({ url: '../classify/classify?url=' + url });
    }
  },
  hotRent(e) {
    let url = e.currentTarget.dataset.jumpUrl
    if (url) {
      my.navigateTo({ url: '../../' + url })
    }
  },
  payToRent(e) {
    let url = e.currentTarget.dataset.jumpUrl
    if (url) {
      my.navigateTo({ url: '../../' + url })
    }
  },
  brandClick(e) {
    my.navigateTo({ url: '../../pagesA/pages/productDetails/productDetails?goodsId=' + e+'&sale_user_id='+app.globalData.sale_user_id })
  },
  onReachBottom() {
    const { allInfo, indexTotal, scrollList, recommendList, recommendTotal } = this.data
    // if (!scrollList.length) {
    //   this.getCateTitle()
    // }
    if (allInfo.length < indexTotal) {
      this.getData(true);
    }
    if (recommendList.length && recommendList.length < recommendTotal) {
      this.getRecomList(true)
    }
  },
  onPullDownRefresh() {
    this.getData()
  },
});
